<?php

namespace App\Traits\Users;

use App\Library\Junda\src\CardService;
use App\Modules\Admin\Entities\Member\UserCardModel;
use App\Modules\Admin\Entities\Member\WaterCardModel;

trait UserCardTrait
{

    public function getUserCardByCardCode($card_no){

        $response_junda_info = CardService::GetICBalance($card_no);
        if (!$response_junda_info['success']){
            $this->error ='没有此卡信息';
            return ['code'=>101,'msg'=>'暂无此卡信息'];
        }
        $response_junda_rows = $response_junda_info['rows'];


//        exit(json_encode($response_junda_rows));

        $Cstate = $response_junda_rows['Cstate'];

        if (!$response_junda_rows['IsElectric']){
            if (!in_array($Cstate,[0])){
                return ['code'=>101,'msg'=>'请您使用实体卡在水机消费一次'];
            }
            if (!in_array($Cstate,[1])){
                return ['code'=>101,'msg'=>'此卡异常，请联系在线客服；'];
            }
        }

        if (!$response_junda_rows['IsUsed']){
            return ['code'=>101,'msg'=>'此卡未开卡，请联系客服'];
        }
        $start = strpos($response_junda_rows['StartDate'], '(') + 1;
        $end = strpos($response_junda_rows['StartDate'], ')', $start);
        $StartDate = (substr($response_junda_rows['StartDate'], $start, $end - $start))/1000;
        $end_start = strpos($response_junda_rows['EndDate'], '(') + 1;
        $endend = strpos($response_junda_rows['EndDate'], ')', $end_start);
        $EndDate = (substr($response_junda_rows['EndDate'], $end_start, $endend - $end_start))/1000;

        $create_info['card_no'] = $card_no;//IC卡号
        $create_info['user_id'] = $this->user_id;//余额
        $create_info['total_balance'] = $response_junda_rows['Cmoney'];//余额
        $create_info['give_money'] = $response_junda_rows['GiveMoney'];//赠送金额剩余
        $create_info['buy_money'] = $response_junda_rows['BuyMoney'];//购买金额剩余
        $create_info['con_money'] = $response_junda_rows['ConMoney'];//总消费金额
        $create_info['nick_name'] = $response_junda_rows['NickName'];// 账户 - 昵称
        $create_info['account_name'] = $response_junda_rows['AccountName'];// 账户
        $create_info['phone'] = $response_junda_rows['AccountName'];//
        $create_info['status'] = $response_junda_rows['Status']?1:0;//
        $create_info['is_electric'] = $response_junda_rows['IsElectric']?1:0;//
        $create_info['cstatus'] = $response_junda_rows['Cstate'];//1.正常卡 2.挂失卡 3.分组卡4.失效卡5.不存在的卡6.异常卡7.金额超限
        $create_info['start_no'] = $response_junda_rows['StartNo'];// 开卡
        $create_info['end_no'] = $response_junda_rows['EndNo'];//
        $create_info['start_date'] = date('Y-m-d',$StartDate);//
        $create_info['end_date'] = date('Y-m-d',$EndDate);//
        $create_info['give_start_date'] = date('Y-m-d',$StartDate);//
        $create_info['give_end_date'] = date('Y-m-d',$EndDate);//
        $create_info['is_used'] =$response_junda_rows['IsUsed'];//
        $create_info['jd_v_card_id'] =$response_junda_rows['Id'];//
        $create_info['created_time'] =time();//

        $total_balance = $response_junda_rows['Cmoney'];
        $user_card_detail = UserCardModel::getInstance()->getUserByCardNo($card_no);
        $card_detail = WaterCardModel::getInstance()->getWaterCardByNo($card_no);
        if ($card_detail){
            WaterCardModel::getInstance()->where(['card_no'=>$card_no])->update($create_info); // TODO: Change the autogenerated stub
            $detail = $create_info;
            $detail['phone'] =  $create_info['phone'];
        }
        if ($user_card_detail){
            $update_user_card['card_id'] = $card_detail['card_id'];
            $update_user_card['card_code'] = $card_no;
            $update_user_card['remaining_value'] = $card_detail['total_balance'];
            $update_user_card['jd_v_card_id'] = $card_detail['jd_v_card_id'];
            $update_user_card['is_electric'] = $card_detail['is_electric'];
            UserCardModel::getInstance()->where(['card_code'=>$card_no])->update($update_user_card); // TODO: Change the autogenerated stub
        }

        if ($total_balance <=1.1){
            return ['code'=>101,'msg'=>'此卡余额不足，请充值'];
        }

        return ['code'=>100,'msg'=>'正常'];

    }


}
